 package edu.upenn.pcr.async;

import java.util.List;

import android.content.Context;
import edu.upenn.pcr.controller.Controller;
import edu.upenn.pcr.controller.DefaultTestingController;
import edu.upenn.pcr.model.async.FetchSectionByInstrAsync;
import edu.upenn.pcr.model.db.PennCourseReviewDBMgr;
import edu.upenn.pcr.model.db.dao.CourseDAO;
import edu.upenn.pcr.model.db.dao.CourseDetailDAO;
import edu.upenn.pcr.model.db.dao.CourseSectionDAO;
import edu.upenn.pcr.model.db.dao.InstructorDAO;
import edu.upenn.pcr.model.db.dao.SemesterDAO;
import edu.upenn.pcr.model.db.entity.Course;
import edu.upenn.pcr.model.db.entity.CourseSection;
import edu.upenn.pcr.model.db.entity.Instructor;
import edu.upenn.pcr.model.db.entity.Semester;
import edu.upenn.pcr.test.AndroidTestCaseBase;

public class FetchInstrCourseAsyncTest extends AndroidTestCaseBase {
	static final String INSTR_ID = "298-SUDIPTO-GUHA";

	Context context;
	InstructorDAO instrDAO;
	CourseDAO courseDAO;
	SemesterDAO semesterDAO;
	CourseDetailDAO courseDetailDAO;
	CourseSectionDAO courseSectionDAO;
	PennCourseReviewDBMgr dbMgr;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		context = this.getTestContext();
		dbMgr = PennCourseReviewDBMgr.getInstance(context);
		instrDAO = dbMgr.getInstructorDAO();
		courseDAO = dbMgr.getCourseDAO();
		semesterDAO = dbMgr.getSemesterDAO();
		courseDetailDAO  = dbMgr.getCourseDetailDAO();
		courseSectionDAO = dbMgr.getCourseSectionDAO();
		
	}

	public void testFetchInstrAsync(){

		Controller instrContr = new DefaultTestingController(context);
		FetchSectionByInstrAsync fetchInstrCourseTask = new FetchSectionByInstrAsync(0, instrContr);

		Instructor instr = new Instructor();
		instr.setFirstName("SUDIPTO");
		instr.setLastName("GUHA");
		instr.setId(INSTR_ID);
		instr.setViewed(0);
		instrDAO.add(instr);
		
		Course course = new Course();
		course.setId("309");
		course.setName("PROG LANG AND TECH II");
		course.setViewed(0);
		courseDAO.add(course);
		
		Semester semester = new Semester();
		semester.setId("2002A");
		semester.setYear(2002);
		semester.setSeason("Spring");
		semester.setName("Spring 2002");
		semesterDAO.add(semester);
		
		fetchInstrCourseTask.test(INSTR_ID);
		
		/*Test updating instructor*/
		assertTrue(instrDAO.count()==1);
		List<Instructor> instrs = instrDAO.findAll();

		for(Instructor in: instrs){
			if(INSTR_ID.equals(in.getId())){ 
				instr = in;
				break;
			}
		}
		assertNotNull(instr);
		assertEquals(instr.getId(), INSTR_ID);
		assertEquals(instr.getViewed(), Integer.valueOf(1));
		
		/*Test populating Course Table*/
		assertTrue(courseDAO.count()>0);
		
		/*Test populating CourseDetail Table*/
		assertTrue(courseDetailDAO.count()>0);
		
		/*Test populating Semester Table*/
		assertTrue(semesterDAO.count()>0);
		
		/*Test updating courseSection*/
		//		List<CourseSection> csList = courseSectionDAO.findByCourseId("309");
		List<CourseSection> csList = courseSectionDAO.findByInstructor(INSTR_ID); 
		assertTrue(csList.size()>0);
	}

}
